package com.imooc.controller.center;

import com.imooc.controller.BaseController;
import com.imooc.pojo.vo.OrderStatusCountsVO;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "用户中心我的订单", tags = {"用户中心我的订单相关接口"})
@RestController
@RequestMapping("myorders")
public class MyOrdersController extends BaseController {


  /**
   * @Author: MoodAir
   * @Description: 获得订单状态数概况
   * @Date: 2020/9/23 0023 23:27
   * @param: userId
   * @return: com.imooc.utils.IMOOCJSONResult
   * @Version: 1.0
   */
  @ApiOperation(value = "获得订单状态数概况", notes = "获得订单状态数概况", httpMethod = "POST")
  @PostMapping("/statusCounts")
  public IMOOCJSONResult statusCounts(
      @ApiParam(name = "userId", value = "用户id", required = true)
      @RequestParam String userId) {
    if (StringUtils.isBlank(userId)) {
      return IMOOCJSONResult.errorMsg(null);
    }
    OrderStatusCountsVO result = myOrdersService.getOrderStatusCounts(userId);
    return IMOOCJSONResult.ok(result);
  }

  /**
   * @Author: MoodAir
   * @Description: 查询订单列表
   * @Date: 2020/9/23 0023 23:27
   * @param: userId
   * @param: orderStatus
   * @param: page
   * @param: pageSize
   * @return: com.imooc.utils.IMOOCJSONResult
   * @Version: 1.0
   */
  @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
  @PostMapping("/query")
  public IMOOCJSONResult query(
      @ApiParam(name = "userId", value = "用户id", required = true)
      @RequestParam String userId,
      @ApiParam(name = "orderStatus", value = "订单状态", required = false)
      @RequestParam Integer orderStatus,
      @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
      @RequestParam Integer page,
      @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
      @RequestParam Integer pageSize) {

    if (StringUtils.isBlank(userId)) {
      return IMOOCJSONResult.errorMsg(null);
    }
    if (page == null) {
      page = 1;
    }
    if (pageSize == null) {
      pageSize = COMMON_PAGE_SIZE;
    }
    PagedGridResult grid = myOrdersService.queryMyOrders(userId, orderStatus, page, pageSize);
    return IMOOCJSONResult.ok(grid);
  }


  /**
   * @Author: MoodAir
   * @Description: 商家发货没有后端，所以这个接口仅仅只是用于模拟
   * @Date: 2020/9/23 0023 23:22
   * @param: orderId
   * @return: com.imooc.utils.IMOOCJSONResult
   * @Version: 1.0
   */
  @ApiOperation(value = "商家发货", notes = "商家发货", httpMethod = "GET")
  @GetMapping("/deliver")
  public IMOOCJSONResult deliver(
      @ApiParam(name = "orderId", value = "订单id", required = true)
      @RequestParam String orderId) throws Exception {

    if (StringUtils.isBlank(orderId)) {
      return IMOOCJSONResult.errorMsg("订单ID不能为空");
    }
    myOrdersService.updateDeliverOrderStatus(orderId);
    return IMOOCJSONResult.ok();
  }


  /**
   * @Author: MoodAir
   * @Description: 用户确认收货
   * @Date: 2020/9/23 0023 23:27
   * @param: orderId
   * @param: userId
   * @return: com.imooc.utils.IMOOCJSONResult
   * @Version: 1.0
   */
  @ApiOperation(value = "用户确认收货", notes = "用户确认收货", httpMethod = "POST")
  @PostMapping("/confirmReceive")
  public IMOOCJSONResult confirmReceive(
      @ApiParam(name = "orderId", value = "订单id", required = true)
      @RequestParam String orderId,
      @ApiParam(name = "userId", value = "用户id", required = true)
      @RequestParam String userId) throws Exception {

    IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
    if (checkResult.getStatus() != HttpStatus.OK.value()) {
      return checkResult;
    }

    boolean res = myOrdersService.updateReceiveOrderStatus(orderId);
    if (!res) {
      return IMOOCJSONResult.errorMsg("订单确认收货失败！");
    }

    return IMOOCJSONResult.ok();
  }

  /**
   * @Author: MoodAir
   * @Description: 用户删除订单
   * @Date: 2020/9/23 0023 23:26
   * @param: orderId
   * @param: userId
   * @return: com.imooc.utils.IMOOCJSONResult
   * @Version: 1.0
   */
  @ApiOperation(value = "用户删除订单", notes = "用户删除订单", httpMethod = "POST")
  @PostMapping("/delete")
  public IMOOCJSONResult delete(
      @ApiParam(name = "orderId", value = "订单id", required = true)
      @RequestParam String orderId,
      @ApiParam(name = "userId", value = "用户id", required = true)
      @RequestParam String userId) throws Exception {

    IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
    if (checkResult.getStatus() != HttpStatus.OK.value()) {
      return checkResult;
    }

    boolean res = myOrdersService.deleteOrder(userId, orderId);
    if (!res) {
      return IMOOCJSONResult.errorMsg("订单删除失败！");
    }

    return IMOOCJSONResult.ok();
  }


  /**
   * @Author: MoodAir
   * @Description: 用于验证用户和订单是否有关联关系，避免非法用户调用
   * @Date: 2020/9/23 0023 23:23
   * @param: userId
   * @param: page
   * @param: pageSize
   * @return: com.imooc.utils.IMOOCJSONResult
   * @Version: 1.0
   */
//    private IMOOCJSONResult checkUserOrder(String userId, String orderId) {
//        Orders order = myOrdersService.queryMyOrder(userId, orderId);
//        if (order == null) {
//            return IMOOCJSONResult.errorMsg("订单不存在！");
//        }
//        return IMOOCJSONResult.ok();
//    }
  @ApiOperation(value = "查询订单动向", notes = "查询订单动向", httpMethod = "POST")
  @PostMapping("/trend")
  public IMOOCJSONResult trend(
      @ApiParam(name = "userId", value = "用户id", required = true)
      @RequestParam String userId,
      @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
      @RequestParam Integer page,
      @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
      @RequestParam Integer pageSize) {

    if (StringUtils.isBlank(userId)) {
      return IMOOCJSONResult.errorMsg(null);
    }
    if (page == null) {
      page = 1;
    }
    if (pageSize == null) {
      pageSize = COMMON_PAGE_SIZE;
    }
    PagedGridResult grid = myOrdersService.getOrdersTrend(userId, page, pageSize);
    return IMOOCJSONResult.ok(grid);
  }
}